package org.vectrics.skill.struts;

import javax.servlet.jsp.JspException;

import org.apache.struts.action.ActionForm;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;


public class ShowEvaluationAction extends VectricsStrutsAction {
    public static String ACTION_NAME = "save_detail_skills";
    
    public String doExecute(VectricsActionData actionData)	throws JspException {
    	String forwardCode = "success";
    	log.debug("execute() called");
    		
    	Person authUser = null;
    	try {
    		authUser = actionData.getAuthPerson();
    	}
    	catch (Exception ex) {
    		log.error("Exception finding auth user", ex);
    		throw new JspException("Exception finding auth user", ex);
    	}
    	
    	ActionForm form = actionData.getForm();
    	if (form == null) {
    		log.debug("execute() - creating form");
    		form = new UserEvaluationForm();
    	}
    	else {
    		log.debug("execute() form was already instantiated, class = "
    				+ form.getClass().getName());
    	}
    	actionData.setRequestForm(form);
    	
    	
		UserEvaluationForm theForm = (UserEvaluationForm)form;
		String evaluationId = actionData.getRequest().getParameter("evaluationId");
		UserEvaluation userEval = null;
		if (evaluationId == null) {
			log.debug("execute() - evaluation id is null, new evaluation assumed");
			Long personId = new Long(actionData.getRequest().getParameter("userId"));
			theForm.setUserId(personId.toString());
			theForm.setEvaluatorId(ThreadProperties.getPerson().getId().toString());
		} else {
			log.debug("execute() - setting form properties");
			userEval = RecruitServiceLocator.getSkillProvider().findEvaluationById(new Long(evaluationId));
			theForm.setId(userEval.getId().toString());
			theForm.setDateCreated(userEval.getDateCreated());
			theForm.setEvaluatorId(userEval.getLeadPerson().getId().toString());
			theForm.setDatePerformed(userEval.getDateStart());
			theForm.setDescription(userEval.getDescription());
			theForm.setNotes(userEval.getNotes());
			theForm.setScore(userEval.getScore().toString());
			theForm.setType(userEval.getType().getId().toString());
			theForm.setUserId(userEval.getPerson().getId().toString());
			try {
    			Person user = userEval.getPerson();
    			if (user != null) {
    				theForm.setUserFullName(user.getName());
    			}
			}
			catch (Exception de) {
				log.error("Exception finding user name", de);
				throw new JspException("Exception finding user name", de);
			}
		}
        return (forwardCode);
    }


}